package com.lihao.wanandroid.app.ext

import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.kingja.loadsir.core.LoadService
import com.kingja.loadsir.core.LoadSir
import com.lihao.wanandroid.R
import me.hgj.jetpackmvvm.demo.app.weight.loadCallBack.EmptyCallback
import me.hgj.jetpackmvvm.demo.app.weight.loadCallBack.ErrorCallback
import me.hgj.jetpackmvvm.demo.app.weight.loadCallBack.LoadingCallback

/**
 * Created by lihao on 2020/6/15.
 * Describe: LoadService状态页函数
 */

/**
 * LoadSir初始化
 */
fun LoadServiceInit(view: View, callback: () -> Unit): LoadService<Any> {
    val loadsir = LoadSir.getDefault().register(view) {
        //点击重试时触发的操作
        callback.invoke()
    }
//    loadsir.showCallback(LoadingCallback::class.java)
    return loadsir
}

/**
 * 显示加载中界面
 */
fun LoadService<*>.showLoading() {
    showCallback(LoadingCallback::class.java)
}

/**
 * 显示错误界面
 */
fun LoadService<*>.showError(error: String) {
    setErrorText(error)
    showCallback(ErrorCallback::class.java)
}

/**
 * 显示空页面
 */
fun LoadService<*>.showEmpty(message: String = "暂时没有数据", resId: Int = R.drawable.load_empty) {
    setEmptyTextAndImage(message, resId)
    showCallback(EmptyCallback::class.java)
}

/**
 * 修改错误信息
 */
fun LoadService<*>.setErrorText(message: String) {
    this.setCallBack(ErrorCallback::class.java) { _, view ->
        view.findViewById<TextView>(R.id.error_text).text = message
    }
}

fun LoadService<*>.setEmptyTextAndImage(message: String, resId: Int) {
    this.setCallBack(EmptyCallback::class.java) { _, view ->
        view.findViewById<TextView>(R.id.tvEmptyText).text = message
        view.findViewById<ImageView>(R.id.ivEmptyImg).setImageResource(resId)
    }

}